<?php

include_once( "{$_SERVER['DOCUMENT_ROOT']}/header.php");

if (isset($_FILES["myfile"])) {
    $ret = array();

    $error = $_FILES["myfile"]["error"];
    //You need to handle  both cases
    //If Any browser does not support serializing of multiple files using FormData() 
    if (!is_array($_FILES["myfile"]["name"])) { //single file
        $fileName = $_FILES["myfile"]["name"];
        $pre_nanme = time() . "_";
        $filename2 = $pre_nanme . $fileName;
        $filename2 = preg_replace("/\s+/", "_", $filename2); //공백제거
        $filename2 = preg_replace("/[ #\&\+\-%@=\/\\\:;,\'\"\^`~\_|\!\?\*$#<>()\[\]\{\}]/i", "_", $filename2);  //특수문자제거
        $filename2 = preg_replace("/\.(php|phtm|htm|cgi|pl|exe|jsp|asp|inc|html|js|c|cpp|h|class|txt|dll|lib)/i", "$0-x", $filename2); //불손한확장자 방어
        $filename3 = urlencode($filename2); //한글파일 변환
        move_uploaded_file($_FILES["myfile"]["tmp_name"], UPLOAD_FOLDER_PHP . $filename2);
        $ret[] = UPLOAD_FOLDER_SCRIPT . $filename2;
    } else {  //Multiple files, file[]
        $fileCount = count($_FILES["myfile"]["name"]);
        for ($i = 0; $i < $fileCount; $i++) {
            $fileName = $_FILES["myfile"]["name"][$i];
            $pre_nanme = time() . "_";
            $filename2 = $pre_nanme . $fileName;
            $filename2 = preg_replace("/\s+/", "_", $filename2); //공백제거
            $filename2 = preg_replace("/[ #\&\+\-%@=\/\\\:;,\'\"\^`~\_|\!\?\*$#<>()\[\]\{\}]/i", "", $filename2);  //특수문자제거
            $filename2 = preg_replace("/\.(php|phtm|htm|cgi|pl|exe|jsp|asp|inc|html|js|c|cpp|h|class|txt|dll|lib)/i", "$0-x", $filename2); //불손한확장자 방어
            $filename3 = urlencode($filename2); //한글파일 변환
            move_uploaded_file($_FILES["myfile"]["tmp_name"][$i], UPLOAD_FOLDER_PHP . $filename2);
            $ret[] = UPLOAD_FOLDER_SCRIPT . $filename3;
        }
    }
    echo json_encode($ret);
}
?>